package com.kdtech.analyse.app;
import java.util.HashMap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * @author dhy
 * @name 甬派
 * @link http://app.cnnb.com.cn/nbdaily/index.html
 * @method http://app.cnnb.com.cn/nbdaily/news/getTopNewsPage 拉取置顶新闻
 * @method http://app.cnnb.com.cn/nbdaily/news/getNewsList 拉取主页新闻
 * @method http://app.cnnb.com.cn/nbdaily/news/getNewsPageEx?channelId=1&order=up&count=20 拉取板块新闻
 * @method http://app.cnnb.com.cn/nbdaily/news/getNewsContent?newsId=[0-9]* 根据 newsId 拉取新闻详细页
 */
public class CnnbAppAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://app.cnnb.com.cn/nbdaily/news/getNewsContent\\?newsId=[0-9]*",
	};
	
	public static HashMap<String, String> getLinks(String url){
		HashMap<String, String> links = new HashMap<String, String>();
		
		try{
			String htmltxt = null;
			if(StringUtils.isBlank(htmltxt))
				return null;
			JSONObject jObj = JSONObject.parseObject(htmltxt);
			System.out.println(jObj.getString("data"));
			JSONArray jArray = JSONArray.parseArray(jObj.getString("data"));
			for(int i = 0; i < jArray.size(); i++){
			try{
					JSONObject data = JSONObject.parseObject(jArray.getString(i));
					String detailUrl = "http://app.cnnb.com.cn/nbdaily/news/getNewsContent?newsId=" + data.getString("newsId");
					links.put(detailUrl, "");
				} catch (Exception e) {
					continue;
				}
			}
		}catch(Exception e){

		}

		System.out.println(links);
		return links;
	}


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Integer clickNum = 0;
		Integer commentNum = 0;

		try{
			JSONObject jObj = JSONObject.parseObject(htmltxt);
			JSONObject data = JSONObject.parseObject(jObj.getString("data"));
			title = data.getString("title");
			date = DateUtils.matchDate(data.getString("time"));
			content = data.getString("body");
			author = data.getString("source");
			clickNum = Integer.parseInt(data.getString("readcount"));
			commentNum = Integer.parseInt(data.getString("commentCount"));
		}catch(Exception e){
			return null;
		}
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);
		news.setClickNum(clickNum);
		news.setCommentNum(commentNum);

		return news;

	}

	

	

	
}

